package com.xinyue.game.client.network;

import com.xinyue.network.codec.GameCodecFactory;
import com.xinyue.network.codec.GameMessageContext;
import com.xinyue.network.hamdlermapping.GameChannelContext;
import com.xinyue.network.message.ChannelHandShakeResponse;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.Attribute;

/**
 * @author 王广帅
 * @date 2021年01月28日 12:54 上午
 */
public class DecodeHandler extends ChannelInboundHandlerAdapter {
    public  DecodeHandler(){
        GameCodecFactory.addMessageClass(ChannelHandShakeResponse.class);
    }
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        Attribute<byte[]> attr = ctx.channel().attr(GameChannelContext.CHANNEL_AES_ENCRYPT_KEY);
        GameMessageContext messageContext = GameCodecFactory.readMessage(byteBuf,attr.get());
        ctx.fireChannelRead(messageContext);
    }
}
